package com.zuorenke.business.router;

import com.zuorenke.business.LessonService;
import com.zuorenke.help.HttpUtils;
import com.zuorenke.help.ParamUtils;
import com.zuorenke.help.SimpleFormatString;
import com.zuorenke.model.Result;
import com.zuorenke.model.po.Lesson;
import com.zuorenke.rabbit.RabbitMQSender;
import com.zuorenke.serialize.support.json.JsonUtils;
import com.zuorenke.vertx.anno.Order;
import com.zuorenke.vertx.anno.Route;
import com.zuorenke.vertx.anno.Router;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

import static java.net.HttpURLConnection.HTTP_OK;

/**
 * @author Xu Haidong
 * @date 2018/8/2
 */
@Component
@Router("restapp")
@Slf4j
public class RestApi {

    @Resource
    RabbitMQSender bindLessonSender;
    
    @Resource
    LessonService lessonService;
    
    /**
     * 演示过滤器
     *
     * @return
     */
    @Route(method = HttpMethod.GET)
    public String firstApi(String value) {
        Result result = new Result();
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
	    bindLessonSender.send(JsonUtils.toJson(result));
        log.info("first api : {}", value);
        return "222";
    }
//
//    @Route(method = HttpMethod.GET, value="/hello")
//    public Integer insertValue(){
//        Calendar cal = Calendar.getInstance();
//        Integer x = new Random(365).nextInt();
//        SimpleDateFormat sdf = new SimpleDateFormat(SimpleFormatString.DATE_FORMAT);
//        cal.add(Calendar.DAY_OF_MONTH, -x);
//        String begin = sdf.format(cal.getTime());
//        cal.add(Calendar.DAY_OF_MONTH, 1);
//        String end = sdf.format(cal.getTime());
//        return  lessonService.getLessonCntByDate(null, null);
//    }
//
//    @Route(method = HttpMethod.GET, value="/other")
//    public Result cache(){
//        return  new Result();
//    }
//
//
//    @Route(method = HttpMethod.GET, value="/getLessonInfo")
//    public Lesson getLessonInfo(){
//        return lessonService.getLessonInfo(6205L);
//    }
//
    /**
     * 演示服务调用
     *
     * @return
     */
//    @Route(value = "/listUsers", method = RouteMethod.GET)
//    public Handler<RoutingContext> listUsers() {
//        return ctx -> {
//            JsonObject param = ParamUtils.getRequestParams(ctx);
//            if (param.containsKey("age")) {
//                param.put("age", Integer.valueOf(param.getString("age")));
//            }
//            User user = new User(param);
//            userAsyncService.listUsers(user, ar -> {
//                if (ar.succeeded()) {
//                    List<User> userList = ar.result();
//                    HttpUtil.fireJsonResponse(ctx.response(), HTTP_OK, ReplyObj.build().setData(userList));
//                } else {
//                    HttpUtil.fireJsonResponse(ctx.response(), HTTP_INTERNAL_ERROR,
//                            ReplyObj.build().setData(ar.cause().getMessage()).setCode(HTTP_INTERNAL_ERROR));
//                }
//            });
//        };
//    }

//    @Route(value = "/findUserById", method = RouteMethod.GET)
//    public Handler<RoutingContext> findUserById() {
//        return ctx -> {
//            JsonObject param = ParamUtils.getRequestParams(ctx);
//            if (!param.containsKey("id")) {
//                HttpUtil.fireJsonResponse(ctx.response(), HTTP_INTERNAL_ERROR,
//                        ReplyObj.build().setMsg("缺少id参数").setCode(HTTP_INTERNAL_ERROR));
//                return;
//            }
//            userTwoAsyncService.findUser(Long.valueOf(param.getString("id")), ar -> {
//                if (ar.succeeded()) {
//                    User user = ar.result();
//                    HttpUtil.fireJsonResponse(ctx.response(), HTTP_OK, ReplyObj.build().setData(user));
//                } else {
//                    HttpUtil.fireJsonResponse(ctx.response(), HTTP_INTERNAL_ERROR,
//                            ReplyObj.build().setData(ar.cause().getMessage()).setCode(HTTP_INTERNAL_ERROR));
//                }
//            });
//        };
//    }

//    @Route(value = "/upload", method = RouteMethod.POST)
//    public Handler<RoutingContext> upload() {
//        return ctx -> {
//            Set<FileUpload> uploads = ctx.fileUploads();
//            FileSystem fs = VertxUtil.getVertxInstance().fileSystem();
//            uploads.forEach(fileUpload -> {
//                String path = "D:/vertxupload/" + fileUpload.fileName();
//                fs.copy(fileUpload.uploadedFileName(), path, ar -> {
//                    if (ar.succeeded()) {
//                        fs.deleteBlocking(fileUpload.uploadedFileName());
//                    }
//                });
//            });
//            HttpUtil.fireJsonResponse(ctx.response(), HTTP_OK, ReplyObj.build().setData("OK"));
//        };
//    }

}
